package com.example.administrator.xiaoxiang.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.activity.DetailPagesActivity;
import com.example.administrator.xiaoxiang.activity.HomeSecondActivity;
import com.example.administrator.xiaoxiang.bean.HomeFragmentBaseBean;
import com.example.administrator.xiaoxiang.bean.HomeGoodGroups;
import com.example.administrator.xiaoxiang.bean.HomeHotBannerBean;

import java.util.List;


/**
 * Created by Administrator on 2017/1/6.
 */

public class HomeContentAdapter extends BaseAdapter{
    private List<HomeFragmentBaseBean> data;
    private Context context;

    public HomeContentAdapter(List<HomeFragmentBaseBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {

        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_home_fragment,parent,false);
            viewHolder = new MyViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView_item_listView_home_fragment);

            viewHolder.recyclerView = (RecyclerView) convertView.findViewById(R.id.myRecycler_item_listView_home_fragment);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (MyViewHolder) convertView.getTag();
        }
        HomeFragmentBaseBean homeFragmentBaseBean = data.get(position);
        if (homeFragmentBaseBean instanceof HomeHotBannerBean) {
            HomeHotBannerBean homeHotBannerBean = (HomeHotBannerBean) homeFragmentBaseBean;
//            Log.e("RDD", "getView: "+homeHotBannerBean.titles );
            viewHolder.imageView.setLayoutParams(new LinearLayout.LayoutParams(100, 50));
            viewHolder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) viewHolder.imageView.getLayoutParams();
            lp.gravity = Gravity.CENTER;

            Glide.with(context).load(homeHotBannerBean.titles).placeholder(R.drawable.home_goods_small_nothing).into(viewHolder.imageView);
            initHot(viewHolder.recyclerView,homeHotBannerBean);

        }else if(homeFragmentBaseBean instanceof HomeGoodGroups){
            final HomeGoodGroups homeGoodGroups = (HomeGoodGroups) homeFragmentBaseBean;
            viewHolder.imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            viewHolder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (homeGoodGroups.id != null) {
                        Intent intent = new Intent(context,HomeSecondActivity.class);
                        intent.putExtra("secondUrl",homeGoodGroups.id);
                        context.startActivity(intent);
                    }
                }
            });
            Glide.with(context).load(homeGoodGroups.thumbnailUrl).placeholder(R.drawable.home_goods_small_nothing).into(viewHolder.imageView);
            initGoods(viewHolder.recyclerView,homeGoodGroups);
        }

        return convertView;
    }

    private void initGoods(RecyclerView recyclerView, HomeGoodGroups homeGoodGroups) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,1, LinearLayoutManager.HORIZONTAL,false);
        recyclerView.setLayoutManager(gridLayoutManager);
        HomeGoodsAdapter adapter = new HomeGoodsAdapter(context,homeGoodGroups.list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new HomeGoodsAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(String url) {
                Intent intent = new Intent(context,DetailPagesActivity.class);
                intent.putExtra("getGoodsCode",url);
//                Toast.makeText(HomeSecondActivity.this, url, Toast.LENGTH_SHORT).show();
                context.startActivity(intent);
            }
        });
    }


    private void initHot(RecyclerView recyclerView,HomeHotBannerBean homeHotBannerBean) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 1, LinearLayoutManager.HORIZONTAL,false);
        recyclerView.setLayoutManager(gridLayoutManager);
        HomeHotAdapter adapter = new HomeHotAdapter(context,homeHotBannerBean.list);
//        Log.e(TAG, "22222222222222222222222initHot: "+Thread.currentThread().getName() );
        recyclerView.setAdapter(adapter);

    }

    class MyViewHolder{
        ImageView imageView;
        RecyclerView recyclerView;
    }
}
